package cn.iocoder.yudao.module.cmt.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cmt.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.base.BaseCommunityDO;
import cn.iocoder.yudao.module.cmt.service.base.BaseCommunityService;

@Tag(name = "管理后台 - 社区信息")
@RestController
@RequestMapping("/cmt/base-community")
@Validated
public class BaseCommunityController {

    @Resource
    private BaseCommunityService baseCommunityService;

    @PostMapping("/create")
    @Operation(summary = "创建社区信息")
    @PreAuthorize("@ss.hasPermission('cmt:base-community:create')")
    public CommonResult<Long> createBaseCommunity(@Valid @RequestBody BaseCommunitySaveReqVO createReqVO) {
        return success(baseCommunityService.createBaseCommunity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新社区信息")
    @PreAuthorize("@ss.hasPermission('cmt:base-community:update')")
    public CommonResult<Boolean> updateBaseCommunity(@Valid @RequestBody BaseCommunitySaveReqVO updateReqVO) {
        baseCommunityService.updateBaseCommunity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除社区信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:base-community:delete')")
    public CommonResult<Boolean> deleteBaseCommunity(@RequestParam("id") Long id) {
        baseCommunityService.deleteBaseCommunity(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除社区信息")
                @PreAuthorize("@ss.hasPermission('cmt:base-community:delete')")
    public CommonResult<Boolean> deleteBaseCommunityList(@RequestParam("ids") List<Long> ids) {
        baseCommunityService.deleteBaseCommunityListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得社区信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:base-community:query')")
    public CommonResult<BaseCommunityRespVO> getBaseCommunity(@RequestParam("id") Long id) {
        BaseCommunityDO baseCommunity = baseCommunityService.getBaseCommunity(id);
        return success(BeanUtils.toBean(baseCommunity, BaseCommunityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得社区信息分页")
    @PreAuthorize("@ss.hasPermission('cmt:base-community:query')")
    public CommonResult<PageResult<BaseCommunityRespVO>> getBaseCommunityPage(@Valid BaseCommunityPageReqVO pageReqVO) {
        PageResult<BaseCommunityDO> pageResult = baseCommunityService.getBaseCommunityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseCommunityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出社区信息 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:base-community:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseCommunityExcel(@Valid BaseCommunityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseCommunityDO> list = baseCommunityService.getBaseCommunityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "社区信息.xls", "数据", BaseCommunityRespVO.class,
                        BeanUtils.toBean(list, BaseCommunityRespVO.class));
    }

}